package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;
    @Override
    public Member findByid(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public Member findByTel(String tel) {
        return memberDao.findByTelephone(tel);
    }

    @Override
    public void addMember(Member member) {
        memberDao.add(member);
    }

    @Override
    public List<Integer> findByMonths(List<String> months) {
        List<Integer> members = new ArrayList<>();
        for (String month : months) {
            int count=memberDao.findMemberCountByMonth(month+"-01");
            members.add(count);
        }
        return members;
    }

    @Override
    public PageResult findByPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        Page<Member> page = PageHelper.startPage(currentPage, pageSize);
        memberDao.findAll();
        long total = page.getTotal();
        return new PageResult(total,page.getResult());
    }

}
